package cn.wh.ymcc.web.controller;

import cn.wh.ymcc.domain.PayOrder;
import cn.wh.ymcc.dto.AlipayNotifyDto;
import cn.wh.ymcc.dto.PayApplyDto;
import cn.wh.ymcc.result.JSONResult;
import cn.wh.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 根据订单号查询订单
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectByOrderNumber(orderNo);
        return payOrder == null?JSONResult.error():JSONResult.success();
    }

    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        String formStr  = payOrderService.apply(dto);
        return JSONResult.success(formStr);
    }

    /**
     * 处理支付成功之后的异步回调逻辑
     * @param dto
     * @return
     */
    @RequestMapping("/alipay/notify")
    public String notify(AlipayNotifyDto dto){
        try {
            System.out.println("支付宝异步回调");
            return payOrderService.notify(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }
}
